<?php
/**
 * 《投资理财》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Invest extends FinanceUtil {
	public $tbl = Constant::tbl_cw_invest;
	
	public $type = array(
		'-' => '请选择',
		'1' => '个人',
		'2' => '公司',

		'9' => '其它',
	);
	
	public $bank = array(
		// '-' => '请选择',
		// '1' => '现金',
		'2' => '招商银行',
		'3' => '中国工商银行',
		'4' => '中国民生银行',
		'5' => '中信银行',
		'6' => '平安信托',
		'7' => '北京银行',
		'8' => '招商证券',
		'11' => '交通银行',

		'9' => '其它',
	);
	
	public $account = array(
		// '-' => '请选择',
		'1' => '孙晓晖',		
		'2' => '孙晓霞',
		'3' => '孙晓晔',	
		'4' => '景荣深圳',
		'5' => '广东景荣',		
		'6' => '鸿景投资',
		
		'9' => '其它',
	);
	
	public $venture = array(
		'-' => '请选择',
		'1' => '存款',
		'2' => '信托',
		'3' => '借款',
		'4' => '股票',	
		'5' => '基金',
		'6' => '债券',
		'7' => '黄金',
		'8' => '风投',
		
		'9' => '其它',
	);
	
	public $day = array(
		'-' => '请选择',
		'7' => '7天',
		'14' => '14天',
		'21' => '21天',
		'30' => '30天',
		'31' => '31天',
		'35' => '35天',
		'42' => '42天',
		'49' => '49天',
	
		'60' => '2个月',
		'90' => '3个月',
	);
		
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在理财',
		'3' => '取消理财',
		'4' => '完成理财',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		
		'bank' => '银行',
		'account' => '帐户',
		'venture' => '类型',
	
		'amount' => '金额',		
		'yield' => '年收益率',
	
		'date' => '申购日期',
		't1' => '开始日期',
		't2' => '结束日期',
		'day' => '理财期限',	
			
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'bank' => '银行',
		'account' => '帐户',
		'venture' => '分类',
		'type' => '类型',
	
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign('day_option', $this->day);
		$controller->assign_by_ref('bank_option', $this->bank);
		$controller->assign_by_ref('venture_option', $this->venture);
	}
	
	function _dd_option() {
		$t1 = strtotime("-3 month");
		// $t2 = time();
		$t2 = strtotime("15 month");

		$arr = array('-'=>'请选择');
		while ($t1 < $t2) {
			$str = date("Y-m", $t2);
			$t2 = strtotime("-1 month", $t2);

			$arr[$str] = $str;
		}

		return $arr;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('pagesize_s', '100');
		$controller->assign('orderby_s', "t2");
		$controller->assign('desc_s', "1");
		
		$ab = array(
			'-' => '请选择',
			'1' => '格式1',		
			'2' => '格式2',
			'3' => '格式3',
		);
		$controller->assign_by_ref('ab_option', $ab);
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'finance/invest/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		
		if ($px == 'date') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['date'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$bank_s = $request['bank_s'];		
		$type_s = $request['type_s'];
		$venture_s = $request['venture_s'];		
		$account_s = $request['account_s'];
		
		$name_s = $request['name_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}	
		
		$where .= $this->_where_x("venture", $request['venture_s']);
		$where .= $this->_where_x("bank", $request['bank_s']);
		
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content like '%" . $name_s . "%')";
		}
		
		$where .= $this->_where_x("account", $request['account_s']);		
				
		if (!empty($date1_s)) {
			$where .= " AND t2 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($_SESSION['id'] == 100 || $_SESSION['id'] == 101) {
			return '';
		}
		
		if ($this->_is_admin()) {
			return " createtime < '2012-04-20'";
		}
		
		if ($this->_audit('125')) {
			return "";
		}

		$emp = $_SESSION['id'];
		
		/*
		if ($this->_audit('161')) {
			return sprintf(" visible = 1 AND (office = '1' OR oper = '%s')", $emp);
		}
		
		if ($this->_audit('162')) {
			return sprintf(" visible = 1 AND (office = '2' OR oper = '%s')", $emp);
		}*/	

		return sprintf(" (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'finance/invest/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'finance/invest/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t2', strtotime('+1 month'));

		$controller->display($request, 'finance/invest/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$account = $request['account'];
		$venture = $request['venture'];
		
		$bank = $request['bank'];
		$type = $request['type'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		
		$yield = $request['yield'];
		$bonus = $request['bonus'];
		
		$date = $request['date'];
		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, account, venture, bank, name, type, currency, amount, yield, bonus, date, t1, t2, day, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $account, $venture, $bank, $name, $type, $currency, $amount, $yield, $bonus, $date, $t1, $t2, $day, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/invest/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$account = $request['account'];
		$venture = $request['venture'];
		
		$bank = $request['bank'];
		$type = $request['type'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		
		$yield = $request['yield'];
		$bonus = $request['bonus'];
		
		$date = $request['date'];
		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', account = '%s', venture = '%s', bank = '%s', name = '%s', type = '%s', currency = '%s',";
		$fmt .= " amount = '%s', yield = '%s', bonus = '%s', date = '%s', t1 = '%s', t2 = '%s', day = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $account, $venture, $bank, $name, $type, $currency, $amount, $yield, $bonus, $date, $t1, $t2, $day, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/invest/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['account_t'] = $this->account[$row['account']];
		$row['venture_t'] = $this->venture[$row['venture']];
		
		$row['bank_t'] = $this->bank[$row['bank']];
		
		$row['t3'] = $this->_t3($row['t2']);
		
		$row['t4'] = $this->_t4($row);
		
		$b1 = ($row['amount'] * $row['yield'] / (365 * 100));
		$row['b1'] = sprintf("%.2f", $b1);

		$row = $this->_view_t($row);

		return $row;
	}
	
	function _t4($row) {
		if ($row['status'] <> '2') {
			return 0;
		}
		
		if (strtotime($row['t1']) > time()) {
			return 0;
		}
		
		$day = $this->date_diff('d', $row['t1'], date("Y-m-d"));
		
		$ret = ($row['amount'] * $day * $row['yield'] / (365 * 100));
		
		return sprintf("%.2f", $ret);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign('t2', strtotime('+1 month'));

		$controller->display($request, 'finance/invest/copy');
		return true;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id'] || $row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按本金',
			'2' => '按收益',
		);
		$controller->assign('num_option', $num);
		
		$controller->assign('groupby_s', '3');
		$controller->assign('orderby_s', '1');
		$controller->assign('desc_s', '1');
		$controller->assign('result_s', '15');

		$controller->display($request, 'finance/invest/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'account');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t2, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t2, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t2, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(bonus) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}
		
	function tj($controller, $request) {		
		$bb = $this->_list_email('124');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '理财信息';
				
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		$cc = array(
			'即将到期'=>array('w' => "status = '2' AND DATE_ADD(t2, INTERVAL -7 DAY) < now()", 'b'=>'id DESC'),	
			'新增信息'=>array('w' => "$d2", 'b'=>'id DESC'),
			'更新信息'=>array('w' => "$d3 AND $dd", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'finance/invest/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function tjw($controller, $request) {		
		$bb = $this->_list_email('124');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '理财信息（周报）';
		
		$d1 = $this->_tj_df("t2", "%Y-%V");
		$d2 = $this->_tj_df("createtime", "%Y-%V");
		$d3 = $this->_tj_df("modifytime", "%Y-%V");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		$cc = array(
			'完成理财'=>array('w' => "$d1 AND status = '4'", 'b'=>'id DESC'),	
			'新增信息'=>array('w' => "$d2", 'b'=>'id DESC'),
			'更新信息'=>array('w' => "$d3 AND $dd", 'b'=>'id DESC'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);
			
		$body = $this->_tj_html($controller, $request, 'finance/invest/tj_list');
				
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'account';
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, SUM(amount) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'account') {
			$row['a'] = $this->account[$row['a']];
		} elseif ($px == 'type') {
			$row['a'] = $this->type[$row['a']];
		} elseif ($px == 'bank') {
			$row['a'] = $this->bank[$row['a']];
		} elseif ($px == 'venture') {
			$row['a'] = $this->venture[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
}
?>